<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BookController extends Controller
{
    //
    //品种首页 显示内容
    public function getIndex(Request $request)
    {    	
        $count = 1;
        //判断是否有搜索
        if($request -> input('keywords')){
            $res = DB::table('bookdetial')
                ->where('mName','like','%'.$request->input('keywords').'%')
                ->orWhere('power','like','%'.$request->input('keywords').'%')
                ->orWhere('from','like','%'.$request->input('keywords').'%')
                ->paginate(10);
        }else{
            //获取所有的信息
            $res = DB::table('bookdetial') -> paginate(10);
            // dd($res);
        }
        //获取所有信息
        $data = $request -> all();
    	return view('Admin.book.index',['varieties' => $res,'$count' => $count,'data' => $data]);
    }

    //品种添加页
    public function  getAdd()
    {
    	return view('Admin.book.add');
    }

    //执行插入
    public function postInsert(Request $request)
    {
    	$data = $request -> except(['_token']);
    	// dd($data);
    	//执行插入
    	$res = DB::table('bookdetial') -> insert($data);
    	if($res){
    		return redirect('/admin/book') -> with('success','品种插入成功');
    	}else{
    		return back() -> with('error','品种插入失败');
    	}
    }

    //品种修改页
    public function getEdit($id)
    {
    	//获取该条信息
    	$res = DB::table('bookdetial') -> where('id',$id) -> first();
    	// dd($res);
    	return view('Admin.book.edit',['varieties' => $res]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
    	$data = $request -> except(['_token','id']);
    	$id = $request -> input('id');
    	// dd($id);
    	// dd($data);
    	//执行修改
    	$res = DB::table('bookdetial') -> where('id',$id) -> update($data);
    	if($res){
    		return redirect('/admin/book') -> with('success','信息修改成功');
    	}else{
    		return back() -> with('error','信息修改失败');
    	}
    }

    //执行删除
    public function getDelete($id)
    {
    	// dd($id);
    	//执行
    	$res = DB::table('bookdetial') -> where('id',$id) -> delete();
    	if($res){
    		return redirect('/admin/book') -> with('success','信息删除成功');
    	}else{
    		return back() -> with('error','信息删除失败');
    	}
    }
}
